//
//  UIAlertController+Lc.m
//  18Birdies
//
//  Created by poleness on 16/6/27.
//  Copyright © 2016年 Lc. All rights reserved.
//

#import "LcFoundation.h"
#import "UIAlertController+Lc.h"
#import "ViewControllerManager.h"

@implementation UIAlertController (Lc)

+ (instancetype)alertForNoticeWithTitle:(NSString*)title {
  LC_CHECK_NOT_EMPTY(title);

  UIAlertController* alertController =
      [UIAlertController alertControllerWithTitle:title
                                          message:nil
                                   preferredStyle:UIAlertControllerStyleAlert];
  [alertController
      addAction:[UIAlertAction
                    actionWithTitle:@"OK"
                              style:UIAlertActionStyleCancel
                            handler:nil]];
  return alertController;
}

+ (instancetype)alertForNoticeWithTitle:(NSString*)title okAlertHandle:(AlertHandle)okAlertHandle {
  LC_CHECK_NOT_EMPTY(title);

  UIAlertController* alertController =
      [UIAlertController alertControllerWithTitle:title
                                          message:nil
                                   preferredStyle:UIAlertControllerStyleAlert];
  [alertController
      addAction:[UIAlertAction
                    actionWithTitle:@"OK"
                              style:UIAlertActionStyleCancel
                            handler:^(UIAlertAction* _Nonnull action) {
                              if (okAlertHandle != nil) {
                                okAlertHandle();
                              }
                            }]];
  return alertController;
}

+ (instancetype)alertForNoticeWithTitle:(NSString*)title
                                message:(NSString*)message
                          okAlertHandle:(AlertHandle)okAlertHandle
                           cancelHandle:(AlertHandle)cancelAlertHandle {
  return [self alertForNoticeWithTitle:title
                               message:message
                              okButton:@"OK"
                          cancelButton:@"Cancel"
                         okAlertHandle:okAlertHandle
                          cancelHandle:cancelAlertHandle];
}

+ (instancetype)alertForNoticeWithTitle:(NSString*)title
                                message:(NSString*)message
                               okButton:(NSString*)okButton
                           cancelButton:(NSString*)cancelButton
                          okAlertHandle:(AlertHandle)okAlertHandle
                           cancelHandle:(AlertHandle)cancelAlertHandle {
  UIAlertController* alertController =
      [UIAlertController alertControllerWithTitle:title
                                          message:message
                                   preferredStyle:UIAlertControllerStyleAlert];
  if (okAlertHandle != nil) {
    UIAlertAction* action = [UIAlertAction actionWithTitle:okButton
                                                     style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction* _Nonnull action) {
                                                     if (okAlertHandle != nil) {
                                                       okAlertHandle();
                                                     }
                                                   }];
    [alertController addAction:action];
  }
  if (cancelAlertHandle != nil) {
    UIAlertAction* action = [UIAlertAction actionWithTitle:cancelButton
                                                     style:UIAlertActionStyleCancel
                                                   handler:^(UIAlertAction* _Nonnull action) {
                                                     if (cancelAlertHandle != nil) {
                                                       cancelAlertHandle();
                                                     }
                                                   }];
    [alertController addAction:action];
  }
  return alertController;
}

#pragma mark - Show ActionSheet Event
+ (void)showActionSheet:(NSString*)title
                   btns:(NSArray*)btnsArray
                handler:(AlertCompleteHandler)handler {
  [UIAlertController showActionSheet:title
                      cancelBtnTitle:@"OK"
                                btns:btnsArray
                             handler:handler];
}

+ (void)showActionSheet:(NSString*)title
         cancelBtnTitle:(NSString*)cancelBtnTitle
                   btns:(NSArray*)btnsArray
                handler:(AlertCompleteHandler)handler {
  [UIAlertController showActionSheet:title
                                 msg:nil
                      cancelBtnTitle:cancelBtnTitle
                                btns:btnsArray
                             handler:handler];
}

+ (void)showActionSheet:(NSString*)title
                    msg:(NSString*)msg
         cancelBtnTitle:(NSString*)cancelBtnTitle
                   btns:(NSArray*)btnsArray
                handler:(AlertCompleteHandler)handler;
{
  [UIAlertController showActionSheet:title
                                 msg:nil
                      cancelBtnTitle:cancelBtnTitle
                     destructivTitle:nil
                                btns:btnsArray
                             handler:handler];
}

+ (void)showActionSheet:(NSString*)title
                    msg:(NSString*)msg
         cancelBtnTitle:(NSString*)cancelBtnTitle
        destructivTitle:(NSString*)destructivTitle
                   btns:(NSArray*)btnsArray
                handler:(AlertCompleteHandler)handler {
  [UIAlertController showActionSheetInViewController:[UIViewController currentViewController]
      withTitle:title
      message:msg
      cancelButtonTitle:cancelBtnTitle
      destructiveButtonTitle:destructivTitle
      otherButtonTitles:btnsArray
#if TARGET_OS_IOS
      popoverPresentationControllerBlock:^(UIPopoverPresentationController* popover) {
      }
#endif
      tapBlock:^(UIAlertController* __nonnull controller, UIAlertAction* __nonnull action,
                 NSInteger buttonIndex) {
        if (handler != nil) {
          if (buttonIndex == controller.cancelButtonIndex) {
            handler(action, UIAlertActionCancel);
          } else if (buttonIndex == controller.destructiveButtonIndex) {
            handler(action, UIAlertActionDestructive);
          } else if (buttonIndex >= controller.firstOtherButtonIndex) {
            handler(action, (long)buttonIndex - controller.firstOtherButtonIndex + UIAlertActionFirstOther);
          }
        }
      }];
}

#pragma mark - Show Alert Event
+ (void)showAlert:(NSString*)title {
  [UIAlertController showAlert:title msg:nil];
}

+ (void)showAlert:(NSString*)title msg:(NSString*)msg {
  [UIAlertController showAlert:title msg:msg cancelBtnTitle:@"OK"];
}

+ (void)showAlertWithMsg:(NSString*)msg {
  [UIAlertController showAlert:nil msg:msg];
}

+ (void)showAlert:(NSString*)title msg:(NSString*)msg cancelBtnTitle:(NSString*)cancelBtnTitle {
  [UIAlertController showAlert:title msg:msg cancelBtnTitle:cancelBtnTitle btns:nil tapBlock:nil];
}

+ (void)showAlert:(NSString*)title msg:(NSString*)msg tapBlock:(AlertCompleteHandler)handler {
  [UIAlertController showAlert:title
                           msg:msg
                cancelBtnTitle:@"确定"
                          btns:nil
                      tapBlock:handler];
}

+ (void)showAlert:(NSString*)title
               msg:(NSString*)msg
    cancelBtnTitle:(NSString*)cancelBtnTitle
              btns:(NSArray*)btnsArray
          tapBlock:(AlertCompleteHandler)handler {
  [UIAlertController
      showAlertInViewController:[UIViewController currentViewController]
                      withTitle:title
                        message:msg
              cancelButtonTitle:cancelBtnTitle
         destructiveButtonTitle:nil
              otherButtonTitles:btnsArray
                       tapBlock:^(UIAlertController* __nonnull controller,
                                  UIAlertAction* __nonnull action, NSInteger buttonIndex) {
                         if (handler != nil) {
                           if (buttonIndex == controller.cancelButtonIndex) {
                             handler(action, UIAlertActionCancel);
                           } else if (buttonIndex >= controller.firstOtherButtonIndex) {
                             handler(action,
                                     (long)buttonIndex - controller.firstOtherButtonIndex + UIAlertActionFirstOther);
                           }
                         }
                       }];
}

/*
-(void) viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  
  if (self.preferredStyle == UIAlertControllerStyleAlert) {
    __weak UIAlertController *pSelf = self;
    
    dispatch_async(dispatch_get_main_queue(), ^{
      CGRect screenRect = [[UIScreen mainScreen] bounds];
      CGFloat screenWidth = screenRect.size.width;
      CGFloat screenHeight = screenRect.size.height;
      
      [pSelf.view setCenter:CGPointMake(screenWidth / 2.0, screenHeight / 2.0)];
      [pSelf.view setNeedsDisplay];
    });
  }
}

-(void) viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  if (self.preferredStyle == UIAlertControllerStyleAlert) {
    __weak UIAlertController *pSelf = self;
    
    dispatch_async(dispatch_get_main_queue(), ^{
      CGRect screenRect = [[UIScreen mainScreen] bounds];
      CGFloat screenWidth = screenRect.size.width;
      CGFloat screenHeight = screenRect.size.height;
      
      [pSelf.view setCenter:CGPointMake(screenWidth / 2.0, screenHeight / 2.0)];
      [pSelf.view setNeedsDisplay];
    });
  }
}
*/

@end
